package cn.edu.xyc.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Random;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.edu.xyc.dao.BaseDao;
import cn.edu.xyc.domain.EntityPojo;
import cn.edu.xyc.domain.User;
import cn.edu.xyc.service.UserService;
import cn.edu.xyc.util.CardID;
import cn.edu.xyc.util.NameUtil;
import cn.edu.xyc.util.Page;

@Service(UserService.USERSERVICENAME)
public class UserServiceImpl implements UserService {

    @Autowired
    private BaseDao<EntityPojo> edao;
    
    
    @Autowired
    private BaseDao<User> udao;

    @Override
    public List<EntityPojo> getinfo() {

	// List<EntityPojo> find = edao.find("from EntityPojo");
	// System.out.println("getinfo=============");
	// List<EntityPojo> find = edao.find("from EntityPojo");
	System.out.println("getinfo=============  已经找出来了");
	// System.out.println(find);
	return null;
    }

    public void Generationinfo() {
	int size = 500;
	Date start = new Date();
	long time01 = start.getTime();
	for (int i = 0; i <= size; i++) {
	    
	    EntityPojo entityPojo = new EntityPojo();
	    entityPojo.setIdcard(CardID.getRandomID());
	    entityPojo.setComanyname("cccsss");
	    entityPojo.setCount(String.valueOf(new Random().nextInt(30)));
	    entityPojo.setMoney(String.valueOf(new Random().nextInt(3000)));
	    entityPojo.setName(NameUtil.getChineseName());
	    
	    edao.saveOrUpdate(entityPojo);
	    // System.out.println("Serializable:========>     " + save);
	    // System.out.println(pojo);
	}

	long time02 = new Date().getTime();
	//SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

	System.out.println("插入" + size + "条记录所要的时间:" + (time02 - time01));
    }

    /*
     * (non-Javadoc)
     * 
     * @see cn.edu.xyc.service.UserService#login(cn.edu.xyc.domain.User)
     */
    @Override
    public User login(User user) {
	HashMap<String, Object> map = new HashMap<String,Object>();
	map.put("username", user.getUsername());
	map.put("password", user.getPassword());
	List<User> find = this.udao.find("from User u where u.username =:username and u.password=:password",map);
	
	return find.isEmpty() ? null : find.get(0);
    }

    /* (non-Javadoc)
     * @see cn.edu.xyc.service.UserService#addinfo(java.util.List)
     */
    @Override
    public void addinfo(List<EntityPojo> getxlslist) {
	
	for (EntityPojo entityPojo : getxlslist) {
	    this.edao.saveOrUpdate(entityPojo);
	}
    }

    /* (non-Javadoc)
     * @see cn.edu.xyc.service.UserService#getinfobypage(int)
     */
    @Override
    public Page<EntityPojo> getinfobypage(int pagenum) {
	int totalRow = 0;
	int pageSize = 10;
	int totalPage = 0;
	
	List<EntityPojo> find = this.edao.find("from EntityPojo", pagenum, pageSize);
	totalRow = this.edao.count("from EntityPojo");
	totalPage = (int) (totalRow / pageSize);
	if (totalRow % pageSize != 0) {
		totalPage++;
	}
	Page<EntityPojo> page = new Page<EntityPojo>(find, pagenum, 10, totalPage, totalRow);
	return page;
    }

    /* (non-Javadoc)
     * @see cn.edu.xyc.service.UserService#delete(java.lang.String)
     */
    @Override
    public void delete(String id) {
	EntityPojo byId = this.edao.getById(EntityPojo.class, id);
	this.edao.delete(byId);
    }

    /* (non-Javadoc)
     * @see cn.edu.xyc.service.UserService#getbyid(java.lang.String)
     */
    @Override
    public EntityPojo getbyid(String id) {
	return this.edao.getById(EntityPojo.class, id);
    }

    /* (non-Javadoc)
     * @see cn.edu.xyc.service.UserService#update(cn.edu.xyc.domain.EntityPojo)
     */
    @Override
    public void update(EntityPojo enPojo) {
	this.edao.update(enPojo);
    }
    
    public void save(EntityPojo enPojo){
	this.edao.save(enPojo);
    }

}
